package com.arvin.skin.core;

import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.icu.text.UFormat;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.arvin.skin.core.support.SkinViewSupport;
import com.arvin.skin.core.utils.SkinPreference;
import com.arvin.skin.core.utils.SkinResources;
import com.arvin.skin.core.utils.SkinThemeUtils;

import java.util.ArrayList;
import java.util.List;

public class SkinAttribute {
    private static final List<String> mAttributes = new ArrayList<>();
    private Typeface typeface;

    public SkinAttribute(Typeface typeface) {
        this.typeface = typeface;
    }

    static {
        mAttributes.add("background");
        mAttributes.add("src");
        mAttributes.add("textColor");
        mAttributes.add("drawableLeft");
        mAttributes.add("drawableTop");
        mAttributes.add("drawableRight");
        mAttributes.add("drawableBottom");
        mAttributes.add("skinTypeface2");
    }

    private List<SkinView> skinViews = new ArrayList<>();

    public void load(View view, AttributeSet attrs) {
        List<SkinPair> skinPairs = new ArrayList<>();
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            String attrName = attrs.getAttributeName(i);
            if (mAttributes.contains(attrName)) {
                String attrValue = attrs.getAttributeValue(i); //这里拿到的是类似于@xxx的字符串
                if (attrValue.startsWith("#")) {
                    continue;  //不处理 写死的
                }

                int resId;
                if (attrValue.startsWith("?")) {
                    int attrId = Integer.parseInt(attrValue.substring(1));
                    resId = SkinThemeUtils.getResId(view.getContext(), new int[]{attrId})[0];
                } else {
                    resId = Integer.parseInt(attrValue.substring(1));
                }

                if (resId != 0) {
                    SkinPair skinPair = new SkinPair(attrName, resId);
                    skinPairs.add(skinPair);
                }
            }
        }
        if (!skinPairs.isEmpty() || view instanceof TextView || view instanceof SkinViewSupport){
            SkinView skinView = new SkinView(view, skinPairs);
            skinView.applySkin(typeface);
            skinViews.add(skinView);
        }

    }

    public void applySkin() {
        for (SkinView skinView : skinViews) {
            skinView.applySkin(typeface);
        }
    }

    public void setTypeface(Typeface typeface) {
        this.typeface = typeface;
    }

    static class SkinPair {
        String attributeName;
        int resId;

        public SkinPair(String attributeName, int resId) {
            this.attributeName = attributeName;
            this.resId = resId;
        }
    }

    static class SkinView {
        View view;
        List<SkinPair> skinPairs;

        public SkinView(View view, List<SkinPair> skinPairs) {
            this.view = view;
            this.skinPairs = skinPairs;
        }

        public void applySkin(Typeface typeface) {
            applySkinTypeface(typeface);
            applySkinViewSupport();
            for (SkinPair skinPair : skinPairs) {
                switch (skinPair.attributeName) {
                    case "background":
                        Object background = SkinResources.getInstance().getBackground(skinPair.resId);
                        if (background instanceof Integer) {
                            view.setBackgroundColor((Integer) background);
                        } else {
                            ViewCompat.setBackground(view, (Drawable) background);
                        }
                        break;
                    case "src":
                        
                        break;
                    case "textColor":
                        int textColor = SkinResources.getInstance().getColor(skinPair.resId);
                        if (view instanceof TextView) {
                            ((TextView) view).setTextColor(textColor);
                        }
                        break;
                    case "skinTypeface2":
                        Typeface typeface2 = SkinResources.getInstance().getTypeface(skinPair.resId);
                        if (view instanceof TextView) {
                            ((TextView) view).setTypeface(typeface2);
                        }
                        break;
                }
            }
        }

        private void applySkinViewSupport() {
            if (view instanceof SkinViewSupport){
                ((SkinViewSupport) view).applySkin();
            }
        }

        private void applySkinTypeface(Typeface typeface) {
            if (view instanceof TextView){
                ((TextView) view).setTypeface(typeface);
            }
        }
    }
}
